package com.syezon.wifikey.speedup;

import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.syezon.wifikey.R;
import com.syezon.wifikey.base.BaseActivity;
import com.syezon.wifikey.home.ResultHelper;
import com.syezon.wifikey.utils.StatusBarUtil;

import butterknife.BindView;
import butterknife.OnClick;

public class SpeedUpActivity extends BaseActivity {

    public static final String ARG_SSID = "ssid";
    public static final String ARG_INIT_SCORE = "init_score";
    public static final String ARG_ADD_PERCENT = "add_percent";
    public static final String ARG_SIGNAL = "signal";

    private static final int MSG_CLEAN_FINISH = 1;
    private static final int MSG_DELAY_FINISH = 3;
    private static final int MSG_NET_FINISH = 2;
    private static final int MSG_SPEEDUP_FINISH = 4;

    private static final long CHECK_DUR = 2000;

    @BindView(R.id.rl_top)
    RelativeLayout mRlTop;

    @BindView(R.id.fl_anim)
    FrameLayout mFlAnim;

    @BindView(R.id.iv_speed_up_rocket_indicator)
    ImageView mIvSpeedUpRockect;

    @BindView(R.id.tv_ssid)
    TextView mTvSsid;

    @BindView(R.id.iv_clean_indicator)
    ImageView mIvCleanIndicator;
    @BindView(R.id.iv_delay_indicator)
    ImageView mIvDelayIndicator;
    @BindView(R.id.iv_net_indicator)
    ImageView mIvNetIndicator;
    @BindView(R.id.iv_speed_up_indicator)
    ImageView mIvSpeedIndicator;

    private String ssid = "";
    private int initScore;
    private int addPercent;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_CLEAN_FINISH:
                    mIvCleanIndicator.clearAnimation();
                    mIvCleanIndicator.setImageResource(R.drawable.app_speedup_check_ok);

                    mIvDelayIndicator.setVisibility(View.VISIBLE);
                    mIvDelayIndicator.startAnimation(AnimationUtils.loadAnimation(mContext, R.anim.rotate));
                    mHandler.sendEmptyMessageDelayed(MSG_DELAY_FINISH, CHECK_DUR);
                    break;
                case MSG_DELAY_FINISH:
                    mIvDelayIndicator.clearAnimation();
                    mIvDelayIndicator.setImageResource(R.drawable.app_speedup_check_ok);

                    mIvNetIndicator.setVisibility(View.VISIBLE);
                    mIvNetIndicator.startAnimation(AnimationUtils.loadAnimation(mContext, R.anim.rotate));
                    mHandler.sendEmptyMessageDelayed(MSG_NET_FINISH, CHECK_DUR);
                    break;
                case MSG_NET_FINISH:
                    mIvNetIndicator.clearAnimation();
                    mIvNetIndicator.setImageResource(R.drawable.app_speedup_check_ok);

                    mIvSpeedIndicator.setVisibility(View.VISIBLE);
                    mIvSpeedIndicator.startAnimation(AnimationUtils.loadAnimation(mContext, R.anim.rotate));
                    mHandler.sendEmptyMessageDelayed(MSG_SPEEDUP_FINISH, CHECK_DUR);
                    break;
                case MSG_SPEEDUP_FINISH:
                    mIvSpeedIndicator.clearAnimation();
                    mIvSpeedIndicator.setImageResource(R.drawable.app_speedup_check_ok);
                    showResult();
                    break;
            }
        }
    };

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.app_speedup_activity;
    }

    @Override
    protected void initViewsAndEvents() {
        StatusBarUtil.setPaddingSmart(this, mRlTop);
        ssid = getIntent().getStringExtra(ARG_SSID);
        initScore = getIntent().getIntExtra(ARG_INIT_SCORE, 0);
        addPercent = getIntent().getIntExtra(ARG_ADD_PERCENT, 0);
        if (TextUtils.isEmpty(ssid) || initScore <= 0 || addPercent <= 0) {
            finish();
            return;
        }
        mTvSsid.setText(ssid);
        startCheck();
    }

    private void startCheck() {
        mFlAnim.setVisibility(View.VISIBLE);
        mIvCleanIndicator.setVisibility(View.VISIBLE);
        mIvCleanIndicator.startAnimation(AnimationUtils.loadAnimation(this, R.anim.rotate));
        mHandler.sendEmptyMessageDelayed(MSG_CLEAN_FINISH, CHECK_DUR);
        mIvSpeedUpRockect.startAnimation(AnimationUtils.loadAnimation(mContext, R.anim.app_speed_up_checking));
    }

    @OnClick({R.id.iv_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }

    private void showResult() {
        mIvSpeedIndicator.clearAnimation();
        mFlAnim.setVisibility(View.GONE);
        Animation animation = AnimationUtils.loadAnimation(mContext, R.anim.app_speed_up_finish);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                double addScore = initScore * addPercent / 100.0;
                int resultScore = (int) (initScore + addScore);
                if (resultScore > 97) {
                    resultScore = 97;
                }
                SpeedUpConfig.saveCurrentScore(ssid, resultScore);
                SpeedUpConfig.addSpeedUpCount(ssid);
                ResultHelper.showSpeedUpResult(mContext, (int) addScore);
                overridePendingTransition(0, 0);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        mIvSpeedUpRockect.startAnimation(animation);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mHandler.removeCallbacksAndMessages(null);
    }
}
